package com.treasure.mall.biz.service.core.tools.impl;

import com.treasure.mall.biz.base.BaseDaoService;
import com.treasure.mall.biz.entity.log.ToolsLogPO;
import com.treasure.mall.biz.repository.log.ToolsLogRepository;
import com.treasure.mall.biz.service.core.tools.ToolsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.MessageFormat;

/**
 *
 * <p>
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.biz.service.core.tools.impl
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/8/22 11:28 上午
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 *
 */
@Service
@Transactional
public class ToolsServiceImpl extends BaseDaoService implements ToolsService {

    @Autowired
    private ToolsLogRepository toolsLogRepository;


    /**
     * 修改用聊天等级
     *
     * @param userId
     * @param level
     */
    @Override
    @Transactional
    public void modifyUserChatLevel(String mobile, Long userId, int level) {
        String sql = MessageFormat.format("CALL modifyChatLevel({0},{1})", level, userId.toString());
        this.executeUpdate(sql);
        insertLog("修改用聊天等级", mobile, "修改用户[" + mobile + "]聊天等级为:L" + level);
    }

    /**
     * 修改店铺名称
     *
     * @param merchantId
     * @param shopName
     */
    @Override
    @Transactional
    public void modifyShopName(String mobile, Long merchantId, String shopName) {
        String sql = "UPDATE zb_merchant SET title='" + shopName + "' WHERE merchant_id =" + merchantId;
        this.executeUpdate(sql);
        insertLog("修改店铺名称", mobile, "修改商户[" + mobile + "]的店铺名称为:" + shopName);
    }

    /**
     * 开启用户288权限
     *
     * @param userId
     */
    @Override
    @Transactional
    public void modifyMerchantPermissions(String mobile, Long userId) {
        String sql = MessageFormat.format("UPDATE zb_user set merchant_pay =1 ,user_type =1,user_level =2 where user_id={0} and user_type =0 and merchant_pay =0", userId.toString());
        this.executeUpdate(sql);
        insertLog("开启用户288权限", mobile, "开启用户[" + mobile + "]商户权限");
    }

    /**
     * 增加店铺关注数
     *
     * @param userId
     * @param count
     */
    @Override
    @Transactional
    public void modifyMerchantFollowCount(String mobile, Long userId, int count) {
        String sql = MessageFormat.format("CALL addfollow({0},{1})", count, userId.toString());
        this.executeUpdate(sql);
        insertLog("增加店铺关注数", mobile, "添加商户[" + mobile + "]店铺的关注数量:" + count);
    }

    /**
     * 修改商家保证金
     *
     * @param merchantId
     * @param amount
     */
    @Override
    @Transactional
    public void modifyMerchantEarnestMoney(String mobile, Long merchantId, BigDecimal amount) {
        String sql = MessageFormat.format("UPDATE zb_merchant set earnest_money = earnest_money+{1} WHERE merchant_id ={0}", merchantId.toString(), amount);
        this.executeUpdate(sql);
        insertLog("修改商家保证金", mobile, "额度:" + amount.toString());

    }

    /**
     * 修改用户为达人
     *
     * @param userId
     */
    @Override
    @Transactional
    public void modifyUserLevel(String mobile, Long userId) {
        String sql = MessageFormat.format("UPDATE zb_user set user_level =3 where user_id={0} and user_type =1 and user_level =2", userId.toString());
        this.executeUpdate(sql);
        insertLog("修改用户为达人", mobile, "修改" + mobile + "为达人");
    }


    @Override
    public void insertLog(String title, String mobile, String bodyContent) {
        try {
            ToolsLogPO po = new ToolsLogPO();
            po.setTitle(title);
            po.setBodyContent(bodyContent);
            po.setMobile(mobile);
            toolsLogRepository.save(po);
        } catch (Exception e) {

        }

    }
}
